<template>
  <!--  元数据管理  -->
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
    >
      <el-form-item label="数据标准名称" prop="name">
        <el-input
          v-model="queryParams.mdName"
          placeholder="请输入数据标准名称"
          clearable
          style="width: 240px"
          maxlength="50"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >查询</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <HkTable
      :table-data="dataList"
      :table-columns="qualtableLabel"
      :show-index="true"
      :operate="true"
      :query-params="queryParams"
      :total="total"
      @changePage="getList"
    >
      <template #type="scope">
        {{ scope.row.type == 0 ? "JSON" : "二维表" }}
      </template>
      <template #operate="scope">
        <el-button
          size="mini"
          type="text"
          @click="handleUpdate(scope.row)"
        >编辑
        </el-button>
        <el-button
          size="mini"
          type="text"
          @click="handleUphold(scope.row)"
        >维护
        </el-button>
        <el-button
          size="mini"
          type="text"
          @click="handleDelete(scope.row)"
        >删除
        </el-button>
      </template>
    </HkTable>

    <!-- 添加或修改角色配置对话框 -->
    <editor
      ref="editRef"
      :title="title"
      :form-data="formData"
      @submitForm="getList"
    />
    <DataEditor
      ref="upholdRef"
      title="字典维护"
      :form="formData"
    />
  </div>
</template>

<script>
import {
  listMeta,
  delMeta,
  metaDetails
} from '@/api/milkyway/metaDataManagement'
import HkTable from '@/components/Hk-Table/index'
import editor from './edit'
import DataEditor from './editData'

export default {
  name: 'MetaDataManagement',
  components: { DataEditor, editor, HkTable },
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10
      },
      dataList: [],
      // 查询的数据
      qualtableLabel: [
        { label: '数据标准名称', param: 'mdName' },
        { label: '标准类型', param: 'type', type: 'slot' },
        { label: '描述信息', param: 'description' }
      ],
      formData: {},
      // 遮罩层
      loading: true,
      // 选中数组

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    getList(val) {
      this.loading = false
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      listMeta(this.queryParams).then((res) => {
        this.dataList = res.list
        this.total = res.total
      })
    },
    // 所有菜单节点数据

    // 表单重置
    reset() {
      this.formData = {
        mdName: undefined,
        type: undefined,
        description: undefined,
        id: undefined,
        createtime: undefined
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 10,
        mdName: undefined
      }
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$refs.editRef.isOpen = true
      this.formData.type = 0
      this.title = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      metaDetails(row.id).then((res) => {
        this.formData = res
      })
      this.reset()
      this.$refs.editRef.isOpen = true

      this.title = '编辑'
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function() {
          return delMeta(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    // 字典维护
    handleUphold(row) {
      this.$refs.upholdRef.show(row.id)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
  .targetTable{
    flex: 1;
    overflow: hidden;
  }
</style>
